#include "UniqueFileNumberValidator.h"
#include "NullPointerException.h"

UniqueFileNumberValidator::UniqueFileNumberValidator(const QString& field, int minimum, int maximum):NumberValidator(field,minimum, maximum, true)
{
	this->model = NULL;
}

UniqueFileNumberValidator::~UniqueFileNumberValidator(void)
{
}

void UniqueFileNumberValidator::setModel(korlapAdmin_model* model)
{
	this->model = model;
}

int UniqueFileNumberValidator::validate(QString& input)
{
	int i = NumberValidator::validate(input);
	if(i != 0)
	{
		return i;
	}

	if(this->model == NULL)
	{
		NullPointerException npe;
		npe.setMessage("Model has not been set");
		throw npe;
	}

	int filenr = input.toInt();
	const Patient* p = model->getPatientManager()->findPatientByFileNumber(filenr);
	if(p != NULL)
	{
		this->message = "This " + this->field + " already exists"; 
		//this->state = -4;
		return -4;
	}
	//this->state = 0;
	
	return 0;
}
